package com.wthink.newMain.newCamera

import android.graphics.drawable.StateListDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.lenzshd.services.GPIOProc
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentNewCameraBinding

class NewCameraFragment : Fragment() {

    private val viewModel by viewModels<NewCameraViewmodel>()

    private lateinit var binding: FragmentNewCameraBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_new_camera,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.oneCamera.buttonDrawable = StateListDrawable()
        binding.fourCamera.buttonDrawable = StateListDrawable()
        binding.nineCamera.buttonDrawable = StateListDrawable()
        binding.sixTeenCamera.buttonDrawable = StateListDrawable()

        binding.fourCamera.isChecked = true

        binding.radioGroup.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.oneCamera->{
                    viewModel.oneCamera()
                }
                R.id.fourCamera->{
                    viewModel.fourCamera()
                }
                R.id.nineCamera->{
                    viewModel.nineCamera()
                }
                R.id.sixTeenCamera->{
                    viewModel.sixTeenCamera()
                }
            }
        }

        viewModel.start()

        binding.cameraView.holder.addCallback(object : SurfaceHolder.Callback {
            private var mCamera: android.hardware.Camera? = null
            private fun getDefaultCamera(): Int {
                val numberOfCameras = android.hardware.Camera.getNumberOfCameras()
                val cameraInfo = android.hardware.Camera.CameraInfo()

                var defaultCameraId = 0

                for (i in 0 until numberOfCameras) {
                    android.hardware.Camera.getCameraInfo(i, cameraInfo)
                    if (cameraInfo.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) {
                        defaultCameraId = i
                        break
                    }
                }
                return defaultCameraId
            }

            override fun surfaceChanged(
                holder: SurfaceHolder?,
                format: Int,
                width: Int,
                height: Int
            ) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder?) {
                mCamera?.stopPreview()
                mCamera?.release()
                mCamera = null
            }

            override fun surfaceCreated(holder: SurfaceHolder?) {
                mCamera = android.hardware.Camera.open(getDefaultCamera())
                mCamera!!.apply {
//                    val parameters = getParameters()
//                    parameters.setPreviewSize(
//                        binding.cameraListView.width,
//                        binding.cameraListView.height
//                    )
                    setPreviewDisplay(holder)
                    startPreview()
                }
            }
        })
    }
}